/**
 * 实现promise
 */
// console.log('promise1')
// new Promise((resolve, reject) => {
//   setTimeout(() => {
//     resolve('promise2')
//   }, 0)
// }).then(res => {
//   console.log(res)
//   console.log('promise3')
// })

class Mypromise{
  constructor(fn) {
    this.val = ''
    this.resolveQueen = []
    this.status = 'pending'
    // this.fn = function(){}
    this.resolve = function (val) {
      this.val = val
      this.status = 'resolved'
      this.resolveQueen.forEach(fn => {
        fn(this.val)
      })
      return this
    }.bind(this)
    this.reject = function (val) {
      this.val = val
      this.status = 'reject'
      return this
    }.bind(this)
    fn(this.resolve, this.reject)
  }
  then(fn1) {
    if (this.status === 'resolved') {
      fn1(this.val)
    } else if (this.status === 'pending') {
      this.resolveQueen.push(fn1)
    }
  }
}
// console.log(new Mypromise(() => {}))
console.log('promise1-1')
new Mypromise((resolve, reject) => {
  setTimeout(() => {
    resolve('promise2-2')
  }, 1000)
}).then(res => {
  console.log(res)
  console.log('promise3-3')
})
